package cn.lyn4ever.order.controller;


import cn.lyn4ever.common.MyApiErrors;
import cn.lyn4ever.common.R;
import cn.lyn4ever.entity.Orders;
import cn.lyn4ever.order.service.OrdersService;
import cn.lyn4ever.tool.Assert;
import com.baomidou.mybatisplus.extension.api.ApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

/**
 * (Orders)表控制层
 *
 * @author lyn4ever
 * @since 2020-04-11 21:47:29
 */
@RestController
//@RequestMapping("orders")
@Api("订单相关的API")
public class OrdersController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private OrdersService ordersService;


    /**
     * 查询用户各类订单的统计
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询用户各类订单的统计")
    @GetMapping("getCount/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", allowEmptyValue = true)
    })
    public R getOrderCountById(@PathVariable("id") Integer id) {
        Map<String, Integer> map = ordersService.getCountById(id);
        if (null != map && map.size() > 0) {
            return R.ok("查询成功").put("data", map);
        }
        return R.error("没有查询到结果");
    }


    /**
     * 查询指定类型的订单
     * 分页
     *
     * @param userid 用户id
     * @param type   订单类型
     * @return
     */
    @ApiOperation(value = "查询指定类型的订单 分页")
    @GetMapping("getOrders/{page}/{limit}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", allowEmptyValue = false),
            @ApiImplicitParam(name = "type", value = "订单类型 0 1 2 3 4 待付款,待发货,待收货,待评价,售后"),
            @ApiImplicitParam(name = "page", value = "页码数"),
            @ApiImplicitParam(name = "limit", value = "页面数量"),
    })
    public R getOrders(@PathParam("userid") String userid, @PathParam("type") String type, @PathVariable("page") Integer page, @PathVariable("limit") Integer limit) {
        List<Orders> list = ordersService.getOrders(userid, type, page, limit);
        if (null != list && list.size() > 0) {
            return R.ok("查询成功").put("data", list);
        }
        return R.error("没有查询到结果");
    }

    @ApiOperation(value = "查询单个订单的详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", required = true),
    })
    @GetMapping("getOneDetail/{id}")
    public R getOneDetail(@PathVariable("id") String id) {
        Assert.NotNull(id, MyApiErrors.VALIDATE_FAILED);
        Orders orders = ordersService.getOneDetail(id);
        Assert.NotNull(orders, MyApiErrors.FAILED);
        return R.ok().put("data", orders);
    }


    @ApiOperation(value = "删除单个订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lsh", value = "订单lsh", required = true),
    })
    @GetMapping("delOne/{lsh}")
    public R delOne(@PathVariable("lsh") String lsh) {
        Assert.NotNull(lsh, MyApiErrors.VALIDATE_FAILED);
        int line = ordersService.delOne(lsh);
        return line > 0 ? R.ok("删除成功") : R.error("操作失败，请稍后再试");
    }
}